package data_structure.mooc;

import data_structure.implement.QueueImp;

/**
 * A special queue based on the "Array" defined by myself
 * Delete or look-up the element in the front of the queue
 * Add the element from the tail of the queue
 * Real-time monitoring length of the queue
 * Deletion operation time complexity:O(n)
 * @param <E>
 */
public class ArrayQueue<E> implements QueueImp<E> {

    private Array<E> queue;

    public ArrayQueue(int capacity){
        queue=new Array<E> (capacity);
    }

    public ArrayQueue(){
        queue=new Array<> ();
    }
    @Override
    public boolean isEmpty() {
        return queue.isEmpty();
    }

    /**
     * add the element to the queue
     * @param e E element
     */
    @Override
    public void enqueue(E e) {
        queue.addLast (e);
    }

    /**
     * delete the first element in the queue
     * @return the deleted element
     */
    @Override
    public E dequeue() {
        return queue.removeFirst ();
    }

    /**
     *
     * @return the first element of the queue
     */
    @Override
    public E getFront() {
        return queue.getFirst();
    }

    @Override
    public int getSize() {
        return queue.getSize ();
    }


    public int getCapacity() {
        return queue.getCapacity();
    }

    @Override
    public String toString() {
         StringBuilder sb = new StringBuilder("queue:");
        sb.append ("front [");
        for(int i=0;i<queue.getSize ();i++){
            sb.append (queue.get (i));
            if(i!=queue.getSize () - 1){
                sb.append (",");
            }
        }
        sb.append ("] tail");

        return sb.toString ();
    }

    public static void main(String[] args) {

      QueueImp<Integer>queue = new LinkedListQueue<> ();
        for(int i=0;i<4;i++)
        {
            queue.enqueue (i);
            System.out.println(queue.toString ());
        }
        for(int i=0;i<4;i++)
        {
            queue.dequeue();
            System.out.println(queue.toString ());
        }

    }
}
